//
//  HomeSettingTableViewCell.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/8.
//

import UIKit
import RxSwift
import RxCocoa

class HomeSettingTableViewCell: TableViewCell {

    public var didseleBlock : ((HomeSettingItem) -> Void)?
    
    override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = .clear
        self.contentView.backgroundColor = .clear
        
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(collectionView)

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(8)
            make.leading.equalToSuperview().offset(20)
            make.centerX.equalToSuperview()
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.leading.equalToSuperview()
            make.centerX.equalToSuperview()
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-8)
        }
    }
    
    func bind(viewModel: HomeSettingCellModel) {
        
        let items = viewModel.items
        
        let width = (UIScreen.main.bounds.width-52)/2
        let height = width * 64.0/162.0
        
        let row = items.count/2
        let mod = items.count%2 > 0 ? 1:0
        let cellHeight = CGFloat(row)*height + CGFloat(mod)*height + CGFloat((row+mod)*12)
        collectionView.snp.updateConstraints { make in
            make.height.equalTo(cellHeight)
        }
        
        Observable.just(items)
            .bind(to: collectionView.rx.items(cellIdentifier: HomeSettingCollectionCell.reuseIdentifier, cellType: HomeSettingCollectionCell.self)) { row, item, cell in
                cell.bind(to: item)
            }.disposed(by: cellDisposeBag)
        
        collectionView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            guard let self = self else { return }
            let item = items[indexPath.row]
            self.didseleBlock?(item)
        }).disposed(by: cellDisposeBag)
    }
    
    private lazy var titleLabel: Label = {
        let lable = Label()
        lable.font = Fonts.systemFont(ofSize: 16)
        lable.text = R.string.localizable.setting_title()
        lable.textColor = R.color.common_3_text()
        return lable
    }()
    
    private lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayoutView)
        collectionView.backgroundColor = .clear
        collectionView.register(HomeSettingCollectionCell.self, forCellWithReuseIdentifier: HomeSettingCollectionCell.reuseIdentifier)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isScrollEnabled = false
        return collectionView
    }()
    
    private lazy var flowLayoutView: UICollectionViewFlowLayout = {

        let width = (UIScreen.main.bounds.width-52)/2
        let height = width * 64.0/162.0
        let flowLayoutView = UICollectionViewFlowLayout()
        flowLayoutView.scrollDirection = .vertical
        flowLayoutView.sectionInset = UIEdgeInsets(top: 0, left: 20, bottom: 12, right: 20)
        flowLayoutView.minimumInteritemSpacing = 0
        flowLayoutView.minimumLineSpacing = 12
        flowLayoutView.itemSize = CGSize(width: width , height: height)
        return flowLayoutView
    }()
}

class HomeSettingCollectionCell: CollectionViewCell {
    
    override func makeUI() {
        
        self.contentView.addSubview(cardView)
        cardView.addSubview(iconImageView)
        cardView.addSubview(titleLabel)
        cardView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        iconImageView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(12)
            make.centerY.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.leading.equalTo(iconImageView.snp.trailing).offset(8)
            make.trailing.equalToSuperview().offset(-12)
        }
    }
    
    func bind(to item: HomeSettingItem) {
        iconImageView.image = item.iconImage
        titleLabel.text = item.title
    }
    
    private lazy var cardView: View = {
        let view = View()
        view.backgroundColor = R.color.equalizer_4_no_chosen()
        view.layer.cornerRadius = 6
        view.layer.masksToBounds = true
        return view
    }()
    
    
    private lazy var iconImageView: ImageView = {
        let imageView = ImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var titleLabel: Label = {
        let lable = Label()
        lable.font = Fonts.mediumSystemFont(ofSize: 14)
        lable.textColor = R.color.common_4_text()
        lable.numberOfLines = 0
        return lable
    }()
}

extension HomeSettingItem {
    
    var title: String {
        switch self {
        case .gesture:
            return R.string.localizable.home_setting_gesture_control_title()
        case .camera:
            return R.string.localizable.home_setting_camera_control_title()
        case .earbuds:
            return R.string.localizable.home_setting_looking_earbuds_title()
        }
    }
    
    var iconImage: UIImage? {
        switch self {
        case .gesture:
            return R.image.ic_gesture()
        case .camera:
            return R.image.ic_camera_control()
        case .earbuds:
            return R.image.ic_earbuds_find()
        }
    }
}
